package com.quanyan.club.service.impl;

import com.quanyan.club.entity.RespClubXpLevelMsg;
import com.quanyan.club.entity.db.TbClubMember;
import com.quanyan.club.entity.db.TbClubMultimedia;
import com.quanyan.club.entity.db.TbClubMultimediaExample;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaAdd;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaAddObj;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaPage;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaUpdate;
import com.quanyan.club.entity.vo.resp.RespClubMultimedia;
import com.quanyan.club.mapper.TbClubMultimediaMapper;
import com.quanyan.club.mq.producer.ClubXpLevelProducer;
import com.quanyan.club.service.ClubMultimediaService;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.constants.MqConstants;
import com.quanyan.common.utils.Limit;
import com.quanyan.common.utils.ObjectUtil;
import com.quanyan.mq.client.reqeust.MqRequestMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by zhang on 16/6/28.
 */
@Service("clubMultimediaService")
public class ClubMultimediaServiceImpl extends BaseServiceImpl implements ClubMultimediaService{
    private final static String UPLOAD_ALBUM = "UPLOAD_ALBUM";
    @Autowired
    private TbClubMultimediaMapper clubMultimediaMapper;

    @Autowired
    private ClubXpLevelProducer clubXpLevelProducer;

    /**
     * 获取俱乐部相册列表
     * @param reqClubMultimediaPage
     * @return
     */
    @Override
    public PageObj<List<RespClubMultimedia>> list(ReqClubMultimediaPage reqClubMultimediaPage) {
        int pageSize = reqClubMultimediaPage.getPageSize();
        int pageNum = reqClubMultimediaPage.getPageNum() + 1;
        int clubId = reqClubMultimediaPage.getClubId();

        int count = this.getClubMultimediaListCount(clubId);

        List<TbClubMultimedia> clubMultimediaList = this.getClubMultimediaList(clubId,pageSize,pageNum);
        List<RespClubMultimedia> list = new ArrayList<>();
        if (clubMultimediaList.isEmpty()) {

        }else{
            RespClubMultimedia respClubMultimedia;
            for (TbClubMultimedia tbClubMultimedia : clubMultimediaList) {
                respClubMultimedia = new RespClubMultimedia();
                respClubMultimedia.setClubId(clubId);
                respClubMultimedia.setCreateTime(tbClubMultimedia.getCreateTime().getTime());
                respClubMultimedia.setMultimediaId(tbClubMultimedia.getId());
                respClubMultimedia.setMultimediaType(tbClubMultimedia.getMultimediaType());
                respClubMultimedia.setMultimediaUrl(tbClubMultimedia.getMultimediaUrl());
                list.add(respClubMultimedia);
            }
        }
        PageObj<List<RespClubMultimedia>> pageObj = PageObj.create(count,pageNum,pageSize,list);
        return pageObj;
    }
    /**
     * 更新俱乐部相册
     * @param reqClubMultimediaUpdate
     * @param uid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean update(ReqClubMultimediaUpdate reqClubMultimediaUpdate, int uid) {
        List<Integer> list = reqClubMultimediaUpdate.getList();
        int updateType = reqClubMultimediaUpdate.getUpdateType();
        int clubId = reqClubMultimediaUpdate.getClubId();
        //判断该成员的权限
        TbClubMember member = super.getClubMember(uid,clubId);
        if(null == member){
            return false;
        } else {
            String roleCode = member.getRoleCode();
            if(Constants.ClubRole.CLUB_ROLE_PRESIDENT.equals(roleCode) ||
                    Constants.ClubRole.ROLE_CLUB_ADMIN.equals(roleCode)||
                    Constants.ClubRole.ROLE_CLUB_FINANCIAL.equals(roleCode)){
                int n = this.updateClubMultimedia(list,updateType);
                return n > 0;
            } else {
                return false;
            }
        }

    }

    /**
     * 添加俱乐部相册
     * @param reqClubMultimediaAddObj
     * @param uid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public boolean add(ReqClubMultimediaAddObj reqClubMultimediaAddObj, int uid) {
        if (reqClubMultimediaAddObj.getList().isEmpty()) {
            return true;
        } else {
            int n = 0;
            List<ReqClubMultimediaAdd> reqClubMultimediaAddList = reqClubMultimediaAddObj.getList();

            for (ReqClubMultimediaAdd reqClubMultimediaAdd : reqClubMultimediaAddList) {
                int i = this.insertClubMultimedia(reqClubMultimediaAdd,uid);
                n = n + i;
            }
            return n > 0;
        }
    }

    /**
     * 删除俱乐部相册
     * @param multimediaUrlId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int delete(int multimediaUrlId) {
        TbClubMultimediaExample example = new TbClubMultimediaExample();
        TbClubMultimediaExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(multimediaUrlId);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        int  total = clubMultimediaMapper.deleteByExample(example);
        if(total<=0){
            return  0;
        }
        return total;
    }

    /**
     * 获取俱乐部多媒体对象List
     * @param clubId
     * @param pageSize
     * @param pageNum
     * @return
     */
    private List<TbClubMultimedia> getClubMultimediaList(int clubId,int pageSize,int pageNum){
        TbClubMultimediaExample example = new TbClubMultimediaExample();
        TbClubMultimediaExample.Criteria criteria = example.createCriteria();

        criteria.andClubIdEqualTo(clubId);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);

        Limit limit = Limit.buildLimit(pageNum,pageSize);
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        List<TbClubMultimedia> clubMultimediaList = this.clubMultimediaMapper.selectByExample(example);

        return clubMultimediaList;
    }

    private int getClubMultimediaListCount(int clubId){
        TbClubMultimediaExample example = new TbClubMultimediaExample();
        TbClubMultimediaExample.Criteria criteria = example.createCriteria();

        criteria.andClubIdEqualTo(clubId);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);

        int count = this.clubMultimediaMapper.countByExample(example);

        return count;
    }

    /**
     *更新俱乐部相册
     * @param list
     * @return
     */
    private int updateClubMultimedia(List<Integer> list,int updateType){
        TbClubMultimedia clubMultimedia = new TbClubMultimedia();
        if(updateType == 1){
            clubMultimedia.setIsDelete(Constants.IS_DELETE_TRUE);
        }
        TbClubMultimediaExample example = new TbClubMultimediaExample();
        TbClubMultimediaExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        int n = this.clubMultimediaMapper.updateByExampleSelective(clubMultimedia,example);
        return n;
    }

    /**
     * 添加俱乐部相册
     * @param reqClubMultimediaAdd
     * @param uid
     * @return
     */
    private int insertClubMultimedia(ReqClubMultimediaAdd reqClubMultimediaAdd,int uid){
        TbClubMultimedia clubMultimedia = new TbClubMultimedia();
        clubMultimedia.setMultimediaUrl(reqClubMultimediaAdd.getMultimediaUrl());
        clubMultimedia.setUid(uid);
        clubMultimedia.setClubId(reqClubMultimediaAdd.getClubId());
        clubMultimedia.setMultimediaType((byte)reqClubMultimediaAdd.getMultimediaType());
        int n = this.clubMultimediaMapper.insertSelective(clubMultimedia);
        //添加俱乐部经验等级
        addClubExp(reqClubMultimediaAdd, uid);
        return n;
    }

    /**
     *  添加俱乐部经验等级
     * @param reqClubMultimediaAdd
     * @param uid
     */
    private void addClubExp(ReqClubMultimediaAdd reqClubMultimediaAdd, int uid) {
        MqRequestMsg mqRequestMsg = new MqRequestMsg();
        RespClubXpLevelMsg respClubXpLevelMsg = new RespClubXpLevelMsg();
        respClubXpLevelMsg.setClubId(reqClubMultimediaAdd.getClubId());
        respClubXpLevelMsg.setUid(uid);
        respClubXpLevelMsg.setExpName(UPLOAD_ALBUM);
        mqRequestMsg.setMsgId(UUID.randomUUID().toString());
        mqRequestMsg.setTopic(MqConstants.CLUB_XP_LEVEL_TOPIC_NAME);
        mqRequestMsg.setFlag(0);
        mqRequestMsg.setTag(MqConstants.CLUB_XP_LEVEL_TAGS_NAME);
        mqRequestMsg.setBody(ObjectUtil.toByteArray(respClubXpLevelMsg));
        clubXpLevelProducer.syncSend(mqRequestMsg);  //向mq发消息
    }
}
